package edu.android.proyecto.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import edu.android.proyecto.R;
import edu.android.proyecto.beans.Categoria;

public class ListCategoryAdapter extends ArrayAdapter<Categoria> {
	
	private ArrayList<Categoria> items = null;
	Categoria cat = null;
	Context ctx= null;
	
	public ListCategoryAdapter(Context context, int textViewResourceId,ArrayList<Categoria> categorias) {
		super(context, textViewResourceId, categorias);
		this.ctx = context;
		this.items = categorias;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		cat = items.get(position);
		
		View v = convertView;
		if(v== null){
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.celda_categoria,null);
		}
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = inflater.inflate(R.layout.celda_categoria,null);
		
		if(cat!=null){
			ImageView imagen = (ImageView) v.findViewById(R.id.imageCat);
			
			if(imagen !=null){
				imagen.setImageBitmap(getBitmapFromAsset(cat.getPath()));
			}
		}
		
		return v;
	}
	
	private Bitmap getBitmapFromAsset(String path)
	{
		Bitmap bitmap = null;
		try
		{
			Log.v("ruta_imagen", path);
			AssetManager assetManager = ctx.getAssets();
			InputStream istr;
			istr = assetManager.open("images/"+path);
			String[] files = assetManager.list("images/");
			System.out.println(files);
			bitmap = BitmapFactory.decodeStream(istr);
			istr.close();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			bitmap = null;
		}
		return bitmap;
	}

}
